<template>
  <div>
    <el-upload
      ref="upload"
      :action="doUpload"
      class="avatar-uploader"
      :headers="{ token: token }"
      list-type="picture-card"
      :auto-upload="true"
      :limit="1"
      accept="image/jpeg,image/gif,image/png,image/bmp"
      :on-success="onSuccess"
      :on-error="uploadError"
    >
      <i slot="default" class="el-icon-plus" />
      <div slot="file" slot-scope="{ file }">
        <label class="el-upload-list__item-status-label">
          <i class="el-icon-upload-success el-icon-check" />
        </label>
        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in" />
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
// import { upload } from '@/api/system/qiniu'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      doUpload: process.env.VUE_APP_BASE_API + '/qiniuConfig/upload',
      fileList: []
    }
  },
  computed: {
    ...mapGetters(['fileUpload', 'token'])
  },
  methods: {
    handleRemove(file) {
      const fileList = this.$refs.upload.uploadFiles
      const index = fileList.findIndex(item => {
        return item.uid === file.uid
      })
      fileList.splice(index, 1)
      this.fileList.splice(file.response.data, 1)
      // this.$refs.upload.clearFiles(file.uid)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleDownload(file) {
    },
    onSuccess(file) {
      this.$message.success(file.msg)
      this.fileList.push(file.data)
    },
    uploadError(file) {

    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
